package com.zj.healthmanage.controller;

import com.zj.healthmanage.entity.PageResult;
import com.zj.healthmanage.entity.QueryPageBean;
import com.zj.healthmanage.entity.Result;
import com.zj.healthmanage.pojo.Checkitem;
import com.zj.healthmanage.service.CheckitemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/checkitem")
@Api(value = "检查项",description = "检查项")
public class CheckItemController {

    @Autowired
    private CheckitemService checkitemService;

    /**
     * 新增检查项
     * @param checkitem
     * @return
     */
    @ApiOperation(value = "新增检查项",httpMethod = "POST")
    @RequestMapping("/addCheckItem")
    public Result addCheckItem(@RequestBody Checkitem checkitem){
        try{
            checkitemService.insertSelective(checkitem);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "新增检查项失败");
        }
        return new Result(true, "新增检查项成功");
    }


    /**
     * 分页查询检查项
     * @param queryPageBean
     * @return
     */
    @ApiOperation(value = "分页查询检查项",httpMethod = "POST")
    @RequestMapping("/queryCheckItemByPage")
    public PageResult queryCheckItemByPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkitemService.queryCheckItemByPage(queryPageBean);
        return pageResult;
    }

    /**
     * 查询单个检查项
     * @param id
     * @return
     */
    @ApiOperation(value = "查询单个检查项",httpMethod = "POST")
    @RequestMapping("/queryCheckItemById")
    public Result queryCheckItemById(Integer id){
        try{
            Checkitem checkitem = checkitemService.selectByPrimaryKey(id);
            return new Result(true, "查询检查项成功",checkitem);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "查询检查项失败");
        }
    }

    /**
     * 查询所有检查项
     * @return
     */
    @ApiOperation(value = "查询所有检查项",httpMethod = "POST")
    @RequestMapping("/queryCheckItem")
    public Result queryCheckItem(){
        try{
            List<Checkitem> checkitemList = checkitemService.queryAll();
            return new Result(true, "查询检查项成功",checkitemList);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, "查询检查项失败");
        }
    }

    /**
     * 删除检查项
     * @param id
     * @return
     */
    @ApiOperation(value = "删除检查项",httpMethod = "POST")
    @RequestMapping("/deleteCheckItemById")
    public Result deleteCheckItemById(@RequestParam(value = "id") Integer id){
        try{
            checkitemService.deleteByPrimaryKey(id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "删除检查项失败");
        }
        return new Result(true, "删除检查项成功");
    }

    /**
     * 编辑检查项
     * @param checkitem
     * @return
     */
    @ApiOperation(value = "编辑检查项",httpMethod = "POST")
    @RequestMapping("/editCheckItem")
    public Result editCheckItem(@RequestBody Checkitem checkitem){
        try{
            checkitemService.updateByPrimaryKeySelective(checkitem);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "编辑检查项失败");
        }
        return new Result(true, "编辑检查项成功");
    }
}
